<?php
namespace app\index\controller;

use think\Db;
use think\Request;
use app\index\model\User;
use app\index\model\Sms;
use think\Cookie;
use think\Validate;

class Index extends \think\Controller
{

    /**
     *
     */
    public function fputcsv()
    {

//      运行这个方法，然后到你的public目录会看到一个导出的tom.csv文件
//      快速实现导出功能示例
        $list = array
        (
            "thinkphp5,123456789123456789,thinkphp7,USA",
            "James,Adrew,Martin,USA",
        );
//        $list  = Sms::where('rand','<>',1)
//                        ->select();
//        dump($list);
//        die();

        $file = fopen("to3999.csv", "w");

//        foreach ($list as $value) {
//            $value =$value->toarray();
             $file88 = fputcsv($file,$list);
//            return 1123555;
//        }

        echo $file88;


        fclose($file);
    }

    public function sms(){
//        echo 2;

        $phone = input('phone');
        $rand = rand(1000,9999);
        $url = "http://api.chanyoo.cn/utf8/interface/send_sms.aspx?username=guomengtao1&password=aa77bb&content=验证码：".$rand."【高血压】&receiver=".$phone;


        $file = file_get_contents($url);

//        echo $file;

//        转换xml结果
        $xml = simplexml_load_string($file);
        $data = json_decode(json_encode($xml),TRUE);

        echo $data['message'];

        if ($data['message']="短信发送成功"){
//            存入数据库
            $sms = Sms::create([
                'phone' => $phone,
                'rand'  => $rand

            ]);
//            echo "存入成功" .$sms ;
        }


    }

    public function login()
    {
        $phone           = input('param.phone');
        $rand            = input('param.rand');
        $logout          = input('param.logout');
        $warning         = "";
        $invite_phone    = "";
        $pass        = input('param.pass');
        $password        = input('param.password');


        if ($pass){
            $warning ="请输入您要 找回密码 的手机号";
        }


        // dump($invite);




        if ($logout) {
            // 退出登录操作
            // 设置Cookie 有效期为 秒
            Cookie::set('phone','',1);
            // $warning ="退出成功";
            return $this->success('退出成功^_^','login');

        }



        // dump($rand);
        // 'rand'  => 'unique:sms,phone='.$phone.'&create='.$data['account']'

        if ($phone) {

            $validate = new Validate([
                'phone'   => 'max:11|number|between:13000000000,18999999999',
                'rand'    => 'require|min:4|number'

            ]);
            $data = [
                'phone'  => $phone,
                'rand'   => $rand
            ];
            if (!$validate->check($data)) {
                // dump($validate->getError());
                $warning = $validate->getError();
            } else{

                // 判断用户是否已存在
                $count = User::where('phone','=',$phone)->count();

                // 判断短信验证码是否过期
                if ($rand=='5555'){
                    // 增加一个免去短信验证码的登录的方法，方便快速测试
                    $rand = 1;
//                    return 569;

                } else {

//                    return 56;

                    $rand  = Sms::where('rand','=',$rand)
                        ->where('phone',$phone)
                        ->whereTime('create_time', 'today')
                        ->count();
                }


                // $rand = 1;

                // dump($invite);

                if (!$count&$rand) {

                    // 提交注册

                    $user = User::create([
                        'phone'   =>  $phone,

                    ]);


                    // 设置Cookie 有效期为 秒
                    Cookie::set('phone',$phone,3600000);
//                    echo "ok";
//                    return "邀请注册成功，邀请人的id是".$invite;
                    return $this->success('注册成功^_^','index');
                } elseif ($rand) {

                    if($pass) {

                        if (!$password) {
                            return $this->success('密码不能为空','login');
                        }
                        User::where('phone',$phone)
                            ->update(['password' => md5($password)]);

                        return $this->success('密码修改成功^_^','login');
                    }
                    // 设置Cookie 有效期为 秒
                    Cookie::set('phone',$phone,3600000);
                    return $this->success('登录成功^_^','index');
                }
                else {
                    $warning ="验证码有误";
                }

            }



        }


        $this->assign('warning', $warning);
        $this->assign('invite_phone', $invite_phone );

        return $this->fetch();
    }


    public function index(){



        // 渲染模板输出
        return $this->fetch();




    }


}
